var CATEGORIES = {
	
	

};

$(function() {

	$('#search .spec1').change(function(){
		$('#search .spec2').show('slow');
	});

	$('#search .spec2').change(function(){
		$('#search .spec3').show('slow');
	});

	$('.popup-spec1').change(function(){
		$('.popup-spec2').show();
	});

	$('.popup-spec2').change(function(){
		$('.popup-spec3').show();
	});

	$('#link_office_equipment').click(function(){
		$('#search .spec1').val('Office Equipment');
		$('#search .spec1').trigger('change');
		$('table#spec_list > tbody > tr').fadeOut('slow');
		$('table#spec_list > tbody > tr.level2').fadeIn('slow');
		return false;
	});

	$('#link_office_supplies').click(function(){
		$('#search .spec2 select').val('Office Supplies');
		$('#search .spec2 select').trigger('change');
		$('table#spec_list > tbody > tr').fadeOut('slow');
		$('table#spec_list > tbody > tr.level3').fadeIn('slow');
		return false;
	});

	// ---------------------------------------------------------------------------------------------------------------
	// New spec
	// ---------------------------------------------------------------------------------------------------------------

	//reset popup new spec -----------------------------------------------------------------
	
		$('#link_new_spec').click(function(){

			//hide notification box
			COMMON.hide_notification('#form_new_spec .box');

			//show "loading" animation
			//$('.popup_new_spec .loader').show();

			//define error handler
			var error_occurred = function() {
				COMMON.show_notification('#form_new_spec .box','box-error','Error occurred. Please try again.',{target:'.popup_new_spec',width:0,height:50});
				$('.popup_new_spec .loader').hide();
			};

			//populate contact types and companies
			/*$.ajax({
				type: 'POST',
				url: 'contacts/ajax_get_new_contact_fields',
				data: { },
				dataType: 'json',
				success: function(return_data) {
					if (return_data) {
						for (i in return_data['contact_types']) {
							$('#form_new_spec .contact_types').html($('#form_new_spec .contact_types').html() + '<label class="choice"><input type="checkbox" value="' + i + '" class="contact_type" /> ' + return_data['contact_types'][i] + '<br /></label>');
						}
						$('.popup_new_spec .loader').hide();
					} else {
						error_occurred();
					}
				},
				error: error_occurred
			});*/
			
			//reset fields to blank
			$('#form_new_spec input[type=text]').val('');
			$('#form_new_spec select').val('');
		});

	//event hander for save new spec -------------------------------------------------------
	
		$('#button_save_new_spec').click(function() {
			
			//hide notification box
			COMMON.hide_notification('#form_new_spec .box');
	
			//prepare the data to be sent
			var form_data = {};
			form_data['name'] = $('#form_new_spec .field_name').val();
			form_data['contact_type_ids'] = [];
			$('#form_new_spec .field_contact_type_ids :checked').each(function(){
				form_data['contact_type_ids'].push($(this).val());
			});
	
			//validate
			if ($.trim(form_data['name'])=='') {
				COMMON.show_notification('#form_new_spec .box','box-error','spec name is required.',{target:'.popup_new_spec',width:0,height:0});
				return false;
			}
	
			//show "loading" animation
			$('.popup_new_spec .loader').show();
			
			//define error handler
			var error_occurred = function() {
				COMMON.show_notification('#form_new_spec .box','box-error','Error occurred. Please try again.',{target:'.popup_new_spec',width:0,height:0});
				$('.popup_new_spec .loader').hide();
			};
							
			//send the data
			$.ajax({
				type: 'POST',
				url: 'contacts/ajax_save_new_spec',
				data: { 'json':JSON.stringify(form_data) },
				dataType: 'text',
				success: function(return_data) {
					if (return_data=='1') {
						$('.popup_new_spec .loader').hide();
						$('.popup_new_spec .nyroModalClose').trigger('click');
					} else {
						error_occurred();
					}
				},
				error: error_occurred
			});
			
			//return false so that the href of the button will not be followed
			return false;
	
		});

});